package com.qfedu.microuser.pointinfo.service.impl;

import com.qfedu.common.core.entity.PointInfo;
import com.qfedu.common.core.entity.PointRule;
import com.qfedu.microuser.pointinfo.mapper.PointInfoMapper;
import com.qfedu.microuser.pointinfo.service.PointInfoService;
import com.qfedu.microuser.pointrule.service.PointRuleService;
import com.qfedu.microuser.user.service.UserService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Classname PointInfoServiceImpl
 * @Description TODO
 * @Date 2025-09-24 14:46
 * @Created by 老任与码
 */
@Service
public class PointInfoServiceImpl implements PointInfoService {
    @Resource
    private PointInfoMapper pointInfoMapper;

    @Resource
    private PointRuleService pointRuleService;

    // 借助懒加载
    @Lazy
    @Resource
    private UserService userService;
    
    @Override
    public void addPointBy(Integer uid, Integer pointType) {

        // PointRule pointRule = pointRuleService.ruleInfoByType(pointType);
        List<PointRule> pointRules = pointRuleService.ruleList();

        PointRule pointRule = pointRules.stream()
                .filter(item -> item.getPointType().equals(pointType))
                .findFirst()
                .orElse(null);

        if (pointRule != null) {
            PointInfo pointInfo = new PointInfo();
            pointInfo.setUid(uid);
            pointInfo.setPointType(pointType);
            pointInfo.setPoint(pointRule.getPoint());
            pointInfoMapper.insert(pointInfo);
        }
        // 修改用户记录的积分
        userService.updatePointById(uid, pointRule.getPoint());
    }


}
